package com.dong.com.love.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dong.com.love.Bean.ShowPic;
import com.dong.com.love.adapter.ShowAdapter;
import com.dong.com.love.api.UrlConfig;
import com.dong.com.love.base.BaseActivity;
import com.dong.com.love.cach.CachCallBack;
import com.dong.com.love.model.InitMoudel;
import com.dong.com.love.model.HeadModel;
import com.dong.com.mvvmdemo.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * 图片详情页面
 * Created by Dong on 2016/11/1.
 */

public class ImageShowActivity extends BaseActivity {

    private int id;
    private ImageView img;
    private String cachName = "show";
    private int cachTime = 24 * 60 * 60 * 1000;
    private Context context;
    private ViewPager viewPager;
    private TextView titleView;
    private List<View> list;
    private ShowAdapter showAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_imagshow);
        context = this;

        Intent intent = getIntent();
        id = intent.getIntExtra("id", 1);
        cachName += id;

        initView();

        initData();
    }

    /*
      初始化View
     */
    private void initView() {
        titleView = (TextView) findViewById(R.id.show_tv);
        viewPager = (ViewPager) findViewById(R.id.show_pager);
    }

    /*
      获取数据
     */
    private void initData() {

        new InitMoudel<ShowPic>(context, ShowPic.class, cachName, cachTime) {
            @Override
            public void getDataFromNet() {
                HeadModel.getShow(context, id, new CachCallBack<ShowPic>(context, cachName) {
                    @Override
                    public void update(ShowPic showPic) {
                        bindData(showPic);
                    }
                });
            }

            @Override
            public void doResult(ShowPic showPic) {
                bindData(showPic);
            }
        }.start();

    }

    /*
      处理数据
     */
    private void bindData(ShowPic showPic) {
        String title = showPic.getTitle();
        titleView.setText(title + "");

        list = new ArrayList<View>();
        List<ShowPic.ListBean> beanlist = showPic.getList();
        for (int i = 0; i < beanlist.size(); i++) {
            View view = View.inflate(this, R.layout.item_show, null);
            ImageView img = (ImageView) view.findViewById(R.id.item_show_img);

            ShowPic.ListBean listBean = beanlist.get(i);
            String src = listBean.getSrc();
            String imgUrl = UrlConfig.BASE_IMG_URL + src;
            if (!"".equals(imgUrl))
                Picasso.with(this)
                        .load(imgUrl)
                        .placeholder(R.drawable.title)
                        .error(R.drawable.title)
                        .into(img);
            list.add(view);
        }

        initAdapter();
    }

    /*
      适配数据
     */
    private void initAdapter() {
        if (showAdapter == null) {
            showAdapter = new ShowAdapter(this, list);
            viewPager.setAdapter(showAdapter);
        }else{
            showAdapter.notifyDataSetChanged();
        }
    }
}
